package org.jin.samples.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jin.samples.model.ExamSubject;
import org.jin.samples.model.vo.CommonMessageResultVO;
import org.jin.samples.model.vo.ExamSubjectVO;
import org.jin.samples.model.vo.ExamUserVO;
import org.jin.samples.service.ISubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class SubjectController {
	
	@Autowired
	private ISubjectService subjectService;
	
/*	@Autowired
	private IManageStudentService manageStudentService;*/
	
	@RequestMapping(value= "/subjects", method=RequestMethod.GET)
	public String toSubject(Model model){
		List<ExamSubject> examSubject = subjectService.findAllSubject();
		model.addAttribute("subjectList", examSubject);
		return "subject/subject";	
	}
	
	@ResponseBody
	@RequestMapping(value= "/subjectById", method = RequestMethod.POST)
	public ExamSubjectVO findSubjectById(Integer subjectId){
		System.out.println("*******subjectById*********");
		ExamSubjectVO examSubjectVO = subjectService.findSubjectById(subjectId);
		return examSubjectVO;
	}
	
	
	@ResponseBody
	@RequestMapping(value = "/saveSubject", method = RequestMethod.POST)
	public CommonMessageResultVO addSubject(ExamSubjectVO vo){
		CommonMessageResultVO result = new CommonMessageResultVO();
		result.setSuccess(false);
		try {
			subjectService.addSubject(vo);
			result.setSuccess(true);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value="updataSubject", method = RequestMethod.POST)
	public CommonMessageResultVO updataSubject(ExamSubjectVO vo){
		CommonMessageResultVO result = new CommonMessageResultVO();
		result.setSuccess(false);
		System.out.println("我是状态修改");
		try {
			System.out.println("vo.getStatus()="+vo.getStatus());
			subjectService.Updata(vo);
			System.out.println("修改成功");
			result.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value="deleteSubject", method =  RequestMethod.POST)
	public CommonMessageResultVO deleteSubject(Integer subId){
		CommonMessageResultVO result = new CommonMessageResultVO();
		result.setSuccess(false);
		try {	
			result.setSuccess(subjectService.deleteSubject(subId));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value="/updataStatus", method = RequestMethod.POST)
	public CommonMessageResultVO UpdataStatus(ExamSubjectVO vo){
		CommonMessageResultVO result = new CommonMessageResultVO();
		result.setSuccess(false);
		try {
			subjectService.UpdataStatus(vo);
			result.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value ="/getAllStudent",method=RequestMethod.POST)
	public Map<String, Object> getAllStudent(Integer subId)throws Exception{
		Map<String,Object> resultMap = new HashMap<String, Object>();
		 List<ExamUserVO> examSubject = subjectService.findAllStudent();
		 List<String> selectedUser = subjectService.getAllUserBuSubjectId(subId);
		resultMap.put("allUser", examSubject);
		resultMap.put("selectedUser",selectedUser);
		return resultMap;
	}
	
	@ResponseBody
	@RequestMapping(value = "/saveSubjectStudents", method=RequestMethod.POST)
	public Map<String, Object> saveSubjectStudents(@RequestParam(name="subjectStudents[]")String[] subjectStudents, 
			Integer subjectId){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("success", false);
		
		System.out.println("***********saveSubjectStudents************");
		
		List<String> list = new ArrayList<String>();
		for(String student : subjectStudents){
			list.add(student);
		}
		
		try {
			subjectService.saveSubjectStudent(list, subjectId);
			resultMap.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultMap;
	}
	
	
}
